package com.bwei.topnews;

import android.content.Intent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bwei.topnews.adapter.MoreChannelAdapter;
import com.bwei.topnews.adapter.MyChannelAdapter;
import com.bwei.topnews.app.MyApplication;
import com.bwei.topnews.fragment.LocalCityFragment;
import com.bwei.topnews.fragment.MenuFragment;
import com.bwei.topnews.fragment.MyFragment;
import com.bwei.topnews.manager.FragmentFactory;
import com.bwei.topnews.utils.CommonUtils;
import com.bwei.topnews.utils.LogUtils;
import com.bwei.topnews.utils.TitileUtils;
import com.bwei.topnews.view.LazyViewPager;
import com.bwei.topnews.view.ViewPagerIndicator;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.umeng.socialize.UMShareAPI;

public class MainActivity extends SlidingFragmentActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener, View.OnClickListener {

    private ViewPagerIndicator viewPagerIndicator;
    private LazyViewPager viewPager;
    private SlidingMenu slidingMenu;
    private ImageView imageView;
    private ImageView iv_channel;
    private TextView pd_title;
    private TextView tv_my_pd;
    private TextView tv_my_bj;
    private LinearLayout linearLayout;
    boolean flag = true;
    private GridView gridView_my_channel;
    private GridView gridView_more_channel;
    private MoreChannelAdapter moreChannelAdapter;
    private MyChannelAdapter myChannelAdapter;
    private FragmentPagerAdapter fragmentPagerAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.ImageUser);
        iv_channel = (ImageView) findViewById(R.id.iv_channel);
        tv_my_pd = (TextView) findViewById(R.id.tv_my_pd);
        tv_my_bj = (TextView) findViewById(R.id.tv_my_bj);
        linearLayout = (LinearLayout) findViewById(R.id.ll_my_add_pd);
        gridView_my_channel = (GridView) findViewById(R.id.gv_one);
        gridView_more_channel = (GridView) findViewById(R.id.gv_two);
        iv_channel.setOnClickListener(this);
        tv_my_bj.setOnClickListener(this);

        // 得到网络请求下来的数据
        // 从shard中获取
        initeData();
        // 设置侧滑
        setSlidingMenu();
        // 设置上边菜单滑动
        setView();
        // 设置适配器
        moreChannelAdapter = new MoreChannelAdapter(this, TitileUtils.moreChannel);
        myChannelAdapter = new MyChannelAdapter(this, TitileUtils.myChannel);

        gridView_more_channel.setAdapter(moreChannelAdapter);
        gridView_my_channel.setAdapter(myChannelAdapter);

        gridView_my_channel.setOnItemClickListener(this);
        gridView_more_channel.setOnItemClickListener(this);
        //长按监听
       gridView_my_channel.setOnItemLongClickListener(this);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch (parent.getId()){
            case R.id.gv_one:
                if(MyApplication.channelLongClick){
                    if(position!=0) {
                        TitileUtils.moreChannel.add(TitileUtils.myChannel.get(position));
                        TitileUtils.myChannel.remove(position);
                        myChannelAdapter.notifyDataSetChanged();
                        moreChannelAdapter.notifyDataSetChanged();
                    }
                }else{
                    CommonUtils.clearSp("myChannel");
                    CommonUtils.clearSp("moreChannel");
                    StringBuilder stringBuilder_my = new StringBuilder();
                    for (int i = 0; i < TitileUtils.myChannel.size(); i++) {
                        stringBuilder_my.append(TitileUtils.myChannel.get(i)+",");
                    }
                    CommonUtils.saveSp("myChannel",stringBuilder_my.toString()+",");
                    StringBuilder stringBuilder_more = new StringBuilder();
                    for (int i = 0; i < TitileUtils.moreChannel.size(); i++) {
                        stringBuilder_more.append(TitileUtils.moreChannel.get(i)+",");
                    }
                    CommonUtils.saveSp("moreChannel",stringBuilder_more.toString());
                    pdHide();
                    fragmentPagerAdapter.notifyDataSetChanged();
                    viewPagerIndicator.setViewPager(viewPager,getSlidingMenu());
                    viewPager.setCurrentItem(position);
                    RotateAnimation rotate = new RotateAnimation(180,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                    rotate.setDuration(500);
                    rotate.setFillAfter(true);
                    iv_channel.startAnimation(rotate);

                }
                break;
            case R.id.gv_two:
                TitileUtils.myChannel.add(TitileUtils.moreChannel.get(position));
                TitileUtils.moreChannel.remove(position);
                // 刷新适配器
                moreChannelAdapter.notifyDataSetChanged();
                myChannelAdapter.notifyDataSetChanged();
                break;
        }
    }
    private void initeData() {
        String myChannel = CommonUtils.getSp("myChannel");
        String moreChannel = CommonUtils.getSp("moreChannel");

        String[] split_my = myChannel.split(",");
        String[] split_more = moreChannel.split(",");
        // 装进自己定义的集合中
        for (int i = 0; i <split_my.length; i++) {
            TitileUtils.myChannel.add(split_my[i]);
        }
        for (int i = 0; i < split_more.length; i++) {
            TitileUtils.moreChannel.add(split_more[i]);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        Toast.makeText(MainActivity.this, data.getStringExtra("city"), Toast.LENGTH_SHORT).show();
        LocalCityFragment localCityFragment = (LocalCityFragment) FragmentFactory.getFragment("news_local");
        localCityFragment.initeCity(data.getStringExtra("city"));

    }
    private void setSlidingMenu() {
        // 设置侧拉条目布局
        setBehindContentView(R.layout.menu_frame);
        // 获取侧拉栏目对象
        slidingMenu = getSlidingMenu();

        /*
         * SlidingMenu.TOUCHMODE_FULLSCREEN全屏触摸有效 SlidingMenu.TOUCHMODE_MARGIN
         * 拖拽边缘有效 SlidingMenu.TOUCHMODE_NONE 不响应触摸事件
         */
        slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

        // 设置内容显示页对应的dp大小
        slidingMenu.setBehindOffsetRes(R.dimen.activity_horizontal_margin);
        // //设置左侧侧拉栏目宽度
        // slidingMenu.setBehindWidth(140);
        // 设置侧拉栏目所在位置
        /*
         * SlidingMenu.LEFT SlidingMenu.LEFT_RIGHT SlidingMenu.RIGHT
         */
        slidingMenu.setMode(SlidingMenu.LEFT);
        // 给侧拉栏目和左侧内容页区分开(加线)
        //slidingMenu.setShadowDrawable(R.mipmap.ic_launcher);
        // 设置线的宽度
        slidingMenu.setShadowWidthRes(R.dimen.activity_vertical_margin);

        // fragment去替换布局中节点
        MenuFragment menuFragment = new MenuFragment();
        // FragmentManager管理者
        getSupportFragmentManager()
                // 开启事物
                .beginTransaction()
                // 通过fragment去替换对应布局
                .replace(R.id.menu, menuFragment, "MENU")
                // 提交事物
                .commit();
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                slidingMenu.toggle(true);
            }
        });
    }
    private void setView() {
        viewPagerIndicator = (ViewPagerIndicator) findViewById(R.id.ViewPagerIndicator);
        viewPager = (LazyViewPager) findViewById(R.id.viewPager);
        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()){
            @Override
            public Fragment getItem(int position) {
                Fragment fragment = FragmentFactory.getFragment(TitileUtils.myChannel.get(position).substring(TitileUtils.myChannel.get(position).indexOf(":") + 1));
                return fragment;
            }

            @Override
            public int getCount() {
                return TitileUtils.myChannel.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return TitileUtils.myChannel.get(position).substring(0, TitileUtils.myChannel.get(position).indexOf(":"));
            }
        };
        viewPager.setAdapter(fragmentPagerAdapter);

        viewPagerIndicator.setViewPager(viewPager, getSlidingMenu());
    }
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        MyApplication.channelLongClick = true;
        myChannelAdapter.notifyDataSetChanged();
        return true;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.iv_channel:
            if (flag) {

                RotateAnimation rotate = new RotateAnimation(0,180, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                rotate.setDuration(500);
                rotate.setFillAfter(true);
                v.startAnimation(rotate);
                pdShow();
                pressDown(linearLayout);
            } else {
                RotateAnimation rotate = new RotateAnimation(180,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                rotate.setDuration(500);
                rotate.setFillAfter(true);
                v.startAnimation(rotate);
                pressUp(linearLayout);

            }
            flag = !flag;
            break;
            case R.id.tv_my_bj:
                if(MyApplication.channelLongClick){
                    MyApplication.channelLongClick=false;
                    myChannelAdapter.notifyDataSetChanged();
                }else{
                    MyApplication.channelLongClick=true;
                    myChannelAdapter.notifyDataSetChanged();
                }
                break;
        }

    }

    private void pdShow() {
        tv_my_pd.setVisibility(View.VISIBLE);
        tv_my_bj.setVisibility(View.VISIBLE);
        linearLayout.setVisibility(View.VISIBLE);
        viewPager.setVisibility(View.GONE);
        viewPagerIndicator.setVisibility(View.GONE);
    }

    private void pdHide() {
        viewPagerIndicator.setVisibility(View.VISIBLE);
        viewPager.setVisibility(View.VISIBLE);
        tv_my_pd.setVisibility(View.GONE);
        tv_my_bj.setVisibility(View.GONE);
        linearLayout.setVisibility(View.GONE);
    }

    private void pressDown(View v){
        TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,-1.0f,Animation.RELATIVE_TO_PARENT,0.0f);
        translateAnimation.setDuration(500);
        v.startAnimation(translateAnimation);
    }
    private void pressUp(View v){
        TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.0f,Animation.RELATIVE_TO_PARENT,-1.0f);
        translateAnimation.setDuration(500);
        v.startAnimation(translateAnimation);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                pdHide();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }


}
